<?php
/**
 * 首页接口
 * Created by PhpStorm.
 * User: Earl <guo@seosite.cn>
 * Date: 2017/9/11
 * Time: 下午3:04
 * Updatetime:
 * updater：
 */

namespace WAdmin\Controller;


class HomeController extends BaseController
{

    public function index($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);


        $user_model = M('user');

        $balance = $user_model->where(['id'=>$this->userInfo->id])->getField('balance');

        $message = M('message')->where(['uid'=>$this->userInfo->id])->order('id desc')->count();



//        $car_status = M('car')->field('status')->where(['uid'=>$this->userInfo->id])->group('status')->select();
        $car_status = M('car')->field('count(*) counts,status')->where(['uid'=>$this->userInfo->id])->group('status')->select();


        $car_model = M('car')->field('count(*) counts,car_model')->where(['uid'=>$this->userInfo->id])->group('car_model')->select();


        //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
        $w = date('w',time());

        $first = 1;

        $sdefaultDate = date('Y-m-d');


        $week_start=strtotime(date('Y-m-d',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days')));

        $week_end=date('Y-m-d 23:59:59',strtotime("$week_start +6 days"));

        $user_seven_time =  strtotime(date('Y-m-d',strtotime('-7 days')));



        $user_map['create_time'] = array('gt',$user_seven_time);


        $user_list = $user_model->field("count(*) num,from_unixtime(create_time,'%Y/%m/%d') time")->where($user_map)->group('time')->select();


        //活跃用户

        $act_list = $user_model->field("id,username,from_unixtime(last_time,'%Y-%m-%d %H:%i') time")->order('last_time desc')->limit(5)->select();


//        var_dump($user_list);

        //余额
//        $balance = M('user')->where(['id'=>$this->userInfo->id])->getField('balance');


        $msg_content  = M('message')->where(['uid'=>$this->userInfo->id])->getField('content');

        $ret_data = [

            'car_status'    =>  $car_status,
            'car_model'     =>  $car_model,
            'seven'         =>  $user_list,
            'act'           =>  $act_list,
            'balance'       =>  empty($balance)?'0.00':$balance,
            'message'       =>  empty($message)?0:$message,
            'message_content'       =>  empty($msg_content)?0:$msg_content
        ];


        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$ret_data);

    }






    /**
     * @desc 所有的汽车、分组数据列表综合
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carListByGroup($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);


        $model = M('car');

        $map['uid'] = $this->userInfo->id;

        if(!empty($param['keyword'])){//搜索

            $map['car_no'] = array('like','%'.$param['keyword'].'%');
            list($list,$page) = _listPage($model,$map,1,null,'id,gid,car_no,headsmall','1000');

            if (!empty($list)){
                foreach ($list as $k=>$v){
                    $list[$k]['_child'] = [];
                }
            }

        }else{
            list($list,$page) = _listPage($model,$map,1,'gid desc','group_concat(id) ids,gid','1000','gid');

            if (!empty($list)){
                foreach ($list as $k=>$v){
                    if (empty($v['gid'])){
                        foreach (explode(',',$v['ids']) as $key=>$val){
                            $list[$k+$key] = $model->field('id,gid,headsmall,car_no')->where(['id'=>$val])->find();
                            $list[$k+$key]['_child'] = [];
                        }
                    }else{
                        $list[$k]['id'] = '';

                        $list[$k]['gid'] = $v['gid'];//群的id


                        $list[$k]['headsmall'] = '';//群的图标
                        $list[$k]['car_no'] = M('car_group')->field('title')->where(['id'=>$v['gid']])->getField('title');

                        $list[$k]['_child'] = [];
                        foreach (explode(',',$v['ids']) as $key=>$val){
                            $list[$k]['_child'][$key] = $model->field('id,headsmall,car_no')->where(['id'=>$val])->find();
                            $list[$k]['_child'][$key]['_child'] = [];
                        }
                        unset($list[$k]['ids']);
                    }
                }
            }

            $all = $model->where($map)->field('id,gid,car_no,headsmall')->select();

            foreach ($all as $all_k => $all_v){
                $all[$all_k]['_child'] = [];
            }
            $first = [
                'id' => '',
                'gid'=> '',
                'car_no' => '全部设备',
                'headsmll'=>'',
                '_child'    =>  $all
            ];
            array_unshift($list,$first);
        }
        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$list);


    }

}